home *** CD-ROM | disk | FTP | other *** search
- Path: news.unt.edu!news
- From: Steve Fogoros <sfogoros@hsc.unt.edu>
- Newsgroups: comp.lang.c
- Subject: Re: ?? How to dump text files to screen ??
- Date: Fri, 15 Mar 1996 23:42:55 -0800
- Organization: University of North Texas Health Science Center
- Message-ID: <314A70FF.4EC8@hsc.unt.edu>
- References: <31430CE8.469E@aol2.com> <4iddva$aic@sue.cc.uregina.ca>
- NNTP-Posting-Host: sfogoros.hsc.unt.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win16; I)
-
- Tristan Psionic wrote:
- TP>
- TP> In <31430CE8.469E@aol2.com>, Neil <neil@aol2.com> writes:
- TP> >I need help -- how can I open up a text file and just dump it
- TP> >all out (in plain text) to the screen?
- TP> Well, you can't do exactly that, but this is pretty close (and pretty raw):
- TP>
- TP> #include <stdio.h>
- TP>
- TP> void main (int argc,char argv[])
-
- I have been reminded a number of times that main returns int and should be declared as
- such. I agree in that script error level checking depends on main returning something to
- indicate sucess or failure. I don't yet know if this is required by ANSI C. I have been
- declaring main as returning void because I call exit() to end a program on both failure
- and sucess. If I don't declare it void I get a compiler warning that the function should
- return a value. Since main is not the function that sets the program return value, I
- don't see why I should put in a return at the end of main just so I can declare it as
- returning int. In this case, since the program ends at main you should declare it
- returning int and return something.
-
- Also, argv[] should be *argv[] or **argv
-
- TP> {
- TP> FILE *fp;
- TP> char s[1000];
-
- For my example, drop the array and add,
-
- int c;
-
- TP> if (argc >= 1)
- TP> {
- TP> fp=fopen(argv[1],"r");
-
- Test fp before using it. If it is equal to NULL you have a failure opening the file.
-
- if(fp == NULL) return 10;
-
- TP> while (gets(s,1000,fp) != NULL)
- TP> {
- TP> printf("%s",s);
- TP> }
-
- Instead of taking a chance that the input string is longer than 1000 chars, you can read
- and output a character at a time.
-
- while((c = getc(fp)) != EOF)
- putc(c,stdout);
-
- TP> fclose(fp);
- TP> }
- TP> else
- TP> {
- TP> printf("usage: dumptext [filename]"
- TP> }
-
- return 0;
-
- TP> }
- TP>
- TP> That should work. I'm not an expert at C or anything, but I do believe that
- TP> there aren't any hugely flawed errors in it. Good luck.
- TP> -tristan
-
- --
- Steve Fogoros, Academic Information Coordinator
- University of North Texas Health Science Center
- sfogoros@hsc.unt.edu
-